package com.java.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import com.java.annotation.Role;
import com.java.context.AuthContext;
import com.java.model.AdminUser;
import com.java.vo.Result;

/**
 * 执行方法时判定权限
 * @author Administrator
 *
 */
public class RoleHandler implements InvocationHandler {

	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		Role roleAnnotation = method.getAnnotation(Role.class);
		if (roleAnnotation != null) {
			Result result = new Result();
			AdminUser user = AuthContext.getCurrentUser();
			if (user == null) {
				return result.fail("没有权限");
			}
			if (!roleAnnotation.value().equals(user.getRole())) {
				return result.fail("权限不足");
			}
		}
		return method.invoke(proxy, args);
	}

}
